﻿
using GoodDotNet.Data;
using GoodDotNet.Data.Models;
using GoodDotNet.Core.Model;
using GoodDotNet.Core.Model.Params;
using GoodDotNet.WebAdmin.HttpContextUser;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using GoodDotNet.Core.Data;

namespace GoodDotNet.WebAdmin.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class LoginController : BaseController
    {
        public LoginController(IUser user, IHttpContextAccessor accessor, IOptions<List<ConnectionString>> conns, ILogger<BaseController> logger, IOptions<Appsettings> appsettings) : base(user, accessor, conns, logger, appsettings)
        {
        }



        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("login")]
        public Result Login([FromBody] WebLoginParam form)
        {
            Result result;
            using (var unitOfWork = new UnitOfWork(_conStr))
            {
                var adminuser = unitOfWork.AdminUserRepository.Get(a => a.UserName == form.UserName);
                if (adminuser == null)
                {
                    result = Error("用户名错误，或者已经被删除！");
                }
                else
                {
                    if (adminuser.Pwd.Equals(form.Password))
                    {
                        var msg = new
                        {
                            User = new UserModel
                            {
                                Id = adminuser.Id,
                                Name = adminuser.UserName,
                                ImageHead = adminuser.ImgHead
                            },
                            Module = "",
                            Token = ""
                        };
                        result = Sucess(msg);
                    }
                    else
                    {
                        result = Error("密码错误");
                    }
                }

            }
            return result;
        }
        /// <summary>
        /// 属性token
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("refreshtoken")]
        public Result refreshtoken([FromBody] SignModel form)
        {
            Result result = new Result();
            return result;
        }
    }
}
